package com.newppjoke.ktnewppjoke.utils;

import android.content.res.AssetManager;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.newppjoke.ktnewppjoke.bean.BottomBar;
import com.newppjoke.ktnewppjoke.bean.Destination;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;

/**
 * Project Name: ktnewppjoke
 * File Name:    AppConfig.java
 * ClassName:    AppConfig
 *
 * Description: TODO.
 *
 * @author My start
 * @date 2022年10月10日 7:21 下午
 *
 * Copyright (c) 2022年,  new Network CO.ltd. All Rights Reserved.
 */
public class AppConfig
{
    private static HashMap<String, Destination> sDestConfig;
    private static BottomBar sBottomBar;


//    public  static HashMap<String,Destination> getsDestConfig(){
//        if (sDestConfig==null){
//            String parseFile = parseFile("destination.json");
//            sDestConfig = JSON.parseObject(parseFile, new TypeReference<HashMap<String, Destination>>()
//            {
//            }.getType());
//        }
//         return sDestConfig;

//    }
    public static HashMap<String, Destination> getDestConfig() {
        if (sDestConfig == null) {
            String content = parseFile("destination.json");
            sDestConfig = JSON.parseObject(content, new TypeReference<HashMap<String, Destination>>() {
            });
        }
        return sDestConfig;
    }



    public static BottomBar getBottomBarConfig(){
        if (sBottomBar==null){
            String parseFile = parseFile("main_tabs_config.json");
            sBottomBar = JSON.parseObject(parseFile, BottomBar.class);
        }
        return sBottomBar;
    }

    private  static  String parseFile(String fileName){

        AssetManager assets = AppGlobals.getApplication()
                                        .getResources()
                                        .getAssets();
        InputStream stream=null;
        BufferedReader reader=null;
        StringBuffer buffer = new StringBuffer();
        try
        {
            stream = assets.open(fileName);
            reader = new BufferedReader(new InputStreamReader(stream));

            String line=null;
            while ((line=reader.readLine())!=null){
                buffer.append(line);
            }


        }
        catch (IOException e)
        {

        }finally
        {
                try
                {
                    if (stream!=null)
                    {
                        stream.close();
                    }

                    if (reader!=null){
                        reader.close();


                    }

                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
        }

        return buffer.toString();
    }


}
